<?php
require_once($_SERVER['DOCUMENT_ROOT'] . '/all.conf.php');
require_once(__PHY_LIB.'/inc/page.inc.php');
require_once(__PHY_LIB.'/conf/nation.conf.php');
require_once(__PHY_LIB.'/class/Experience.class.php');

$rReq = validate($_GET, 'nation:e[mala,singapore,thailand]|id:n|category:u|target:u|pg:n');
$oTpl->assign('rNation', $_NATION[$rReq['nation']]);

/// one detail ///
if (0 < $rReq['id']) {
	$oDetail = new Experience($rReq['id']);
	$oTpl->assign('rDetail', $oDetail->getData());
}

/// category list ///
$oList = new ExperienceList();
$oList->cfgField("DISTINCT category");
$oList->cfgWhere("nation='".addslashes($rReq['nation'])."'");
$oList->load();
$oTpl->assign('rCategorys', $oList->fetchArray());
if (0 < $rReq['id']) {
	$rReq['category'] = $oDetail->get('category');
}
elseif (!$rReq['category'] && ($oOne = $oList->nextItem())) {
	$rReq['category'] = $oOne->get('category');
}

/// target list ///
$oList = new ExperienceList();
$oList->cfgField("DISTINCT target");
$oList->cfgWhere("nation='".addslashes($rReq['nation'])."'".($rReq['category'] ? " AND category='".addslashes($rReq['category'])."'" : ""));
$oList->load();
$oTpl->assign('rTargets', $oList->fetchArray());
if (0 < $rReq['id']) {
	$rReq['target'] = $oDetail->get('target');
}
elseif (!$rReq['target'] && ($oOne = $oList->nextItem())) {
	$rReq['target'] = $oOne->get('target');
}

/// list ///
if (!(0 < $rReq['id'])) {
	$rPager = array(
		'Sze' => 10,
		'Req' => 'pg',
		'Num' => max(1, $rP['pg']),
		'Bse' => http_build_query(array_slice($rReq, 0, -1)),
	);
	$oList = new ExperienceList();
	$oList->cfgWhere("nation='".addslashes($rReq['nation'])."'".($rReq['category'] ? " AND category='".addslashes($rReq['category'])."'".($rReq['target'] ? " AND target='".addslashes($rReq['target'])."'" : "") : ""));
	$rPager['Cnt'] = $oList->countAll();
	$rPager['Max'] = ceil($rPager['Cnt']/$rPager['Sze']);
	$oList->cfgOrder("id DESC");
	$oList->load();
	$oTpl->assign('rList', $oList->fetchArray());
	$oTpl->assign('rPager', $rPager);
}

/// html ///
$oTpl->assign('rReq', $rReq);
$oTpl->display(dirname(__FILE__).'/'.basename(__FILE__, '.php').".tpl");
?>
